package com.windgo.controller.admin;


import com.windgo.bean.MarketCategory;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.MarketCategoryInfoVO;
import com.windgo.bean.vo.MarketCategoryL1VO;
import com.windgo.bean.vo.MarketCategoryWithChildrenList;
import com.windgo.service.CategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 查询所有商品类目信息
     *
     * @author mabai
     * @since 2022/10/12 23:51
     */
    @RequiresPermissions("perms[admin:category:list]")
    @RequestMapping("list")
    // 该层通过BaseRespVo返回data、errmsg、errno三个数据
    public BaseRespVo list() {
        // errmsg、errno已有，data数据需要service层提供数据
        CommonData<MarketCategoryInfoVO> data = categoryService.listCategory();
        return BaseRespVo.ok(data);
    }

    /**
     * 查询一级类目商品信息
     *
     * @author mabai
     * @since 2022/10/13 13:51
     */


    @RequestMapping("l1")
    public BaseRespVo l1() {
        CommonData<MarketCategoryL1VO> data = categoryService.listCategoryL1();
        return BaseRespVo.ok(data);
    }


    /**
     * 新增商品类目信息
     *
     * @return com.windgo.bean.common.BaseRespVo
     * @author mabai
     * @since 2022/10/15 12:00
     */
    @PostMapping("create")
    @RequiresPermissions("perms[admin:category:create]")
    public BaseRespVo create(@RequestBody MarketCategory marketCategory) {
        MarketCategory data = categoryService.create(marketCategory);
        return BaseRespVo.ok(data);
    }

    /**
     * 修改商品类目信息
     *
     * @param marketCategoryWithChildrenList
     * @return com.windgo.bean.common.BaseRespVo
     * @author mabai
     * @since 2022/10/15 12:22
     */
    @PostMapping("update")
    @RequiresPermissions("perms[admin:category:update]")
    public BaseRespVo update(@RequestBody MarketCategoryWithChildrenList marketCategoryWithChildrenList) {
        categoryService.updateCategoryInfo(marketCategoryWithChildrenList);
        return BaseRespVo.ok();
    }

    /**
     * 逻辑删除商品类目信息
     *
     * @param marketCategoryWithChildrenList
     * @return com.windgo.bean.common.BaseRespVo
     * @author mabai
     * @since 2022/10/15 12:24
     */
    @PostMapping("delete")
    @RequiresPermissions("perms[admin:category:delete]")
    public BaseRespVo delete(@RequestBody MarketCategoryWithChildrenList marketCategoryWithChildrenList) {
        categoryService.logicDelete(marketCategoryWithChildrenList);
        return BaseRespVo.ok();
    }
}

